package com.lark.algorithm.study.rb;

/**
 * @author btmood
 * @version 1.0
 * @apiNote 红黑树
 * @since 2024-05-06 22:12
 */
public class RBTree<K extends Comparable<K>, V>{

    /**
     * 定义常量，防止红色、黑色 布尔值搞混
     */
    // 红色
    private static final boolean RED = true;
    // 黑色
    private static final boolean BLACK = false;
    private class Node {
        public K key;
        public V value;
        public Node left, right;
        // 用来标记节点的颜色
        public boolean color;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            left = null;
            right = null;
            /**
             * 节点初始化成红色
             * 为什么初始化成红色呢？所有的新节点都设置成红色，后面再做业务处理
             */
            color = RED;
        }
    }

    private Node root;
    private int size;

    public RBTree() {
        root = null;
        size = 0;
    }

    private boolean isRed(Node node) {
        return node.color == RED;
    }

    /**
     * 红黑树的左旋转
     *        node
     *       /   \
     *     T1     x
     *          /   \
     *        T2    T3
     *
     *      左旋转
     *
     *         x
     *       /   \
     *   node    T3
     *   /   \
     * T1    T2
     */
    private Node leftRotate(Node node) {
        Node x = node.right;

        // 左旋转
        node.right = x.left;
        x.left = node;

        // 维持节点颜色
        x.color = node.color;
        node.color = RED;

        return x;
    }

    /**
     * 右旋转
     * @param node
     * @return
     */
    private Node rightRotate(Node node) {
        Node x = node.left;

        //右旋转
        node.left = x.right;
        x.right = node;

        x.color = node.color;
        node.color = RED;

        return x;
    }

    //颜色翻转
    private void flipColors(Node node){
        node.color = RED;
        node.left.color = BLACK;
        node.right.color = BLACK;
    }


    /**
     * 向红黑树添加元素
     * 其实添加元素和往BST添加元素是一样的
     * @param key
     * @param value
     */
    public void add(K key, V value) {
        root = add(root, key, value);
        /**
         * 添加完节点后，保持根节点为黑色
         */
        root.color = BLACK;
    }

    // 向以node为根的红黑树树中插入元素(key, value) 递归算法
    // 返回插入新节点的红黑树的根
    private Node add(Node node, K key, V value) {
        if (node == null) {
            size++;
            return new Node(key, value); // 新节点默认是红色的
        }
        if (key.compareTo(node.key) < 0) {
            node.left = add(node.left, key, value);
        } else if (key.compareTo(node.key) > 0) {
            node.right = add(node.right, key, value);
        } else {
            // key.compareTo(node.key) == 0
            // 如果key相等，就让值等于新的值即可
            node.value = value;
        }

        /**
         * 左旋转的情况
         */
        if (isRed(node.right) && !isRed(node.left)) {
            node = leftRotate(node);
        }
        /**
         * 右旋转的情况
         */
        if (isRed(node.left) && isRed(node.left.left)) {
            node = rightRotate(node);
        }
        /**
         * 颜色反转
         */
        if (isRed(node.left) && isRed(node.right)) {
            flipColors(node);
        }

        return node;
    }

    // 返回以Node为根节点的二分搜索树中，key所在的节点
    private Node getNode(Node node, K key) {
        if (node == null) {
            return null;
        }
        if (key.compareTo(node.key) < 0) {
            return getNode(node.left, key);
        } else if (key.compareTo(node.key) > 0) {
            return getNode(node.right, key);
        } else {
            // key.compareTo(node.key) == 0
            return node;
        }
    }

    public V remove(K key) {
        Node node = getNode(root, key);
        if (node == null) {
            return null;
        }
        remove(root, key);
        return node.value;
    }

    // 删除以node为根的二分搜索树中key为key的元素
    // 返回删除节点后新的二分搜索树的根
    private Node remove(Node node, K key) {
        if (node == null) {
            return null;
        }
        if (key.compareTo(node.key) < 0) {
            node.left = remove(node.left, key);
            return node;
        } else if (key.compareTo(node.key) > 0) {
            node.right = remove(node.right, key);
            return node;
        } else {
            // key.compareTo(node.key) == 0
            if (node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }
            if (node.right == null) {
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }
            Node successors = minimum(node.right);
            successors.right = removeMin(node.right);
            successors.left = node.left;

            node.left = node.right = null;
            return successors;
        }
    }

    private Node minimum(Node node) {
        if (node.left == null) {
            return node;
        }
        return minimum((node.left));
    }

    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    public boolean contains(K key) {
        return getNode(root, key) != null;
    }

    public V get(K key) {
        return getNode(root, key).value;
    }

    public void set(K key, V value) {
        Node node = getNode(root, key);
        if (node == null) {
            throw new IllegalArgumentException(key + " is not in the map");
        }
        node.value = value;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }
}
